package com.example.modulepicselector.adapter;



import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.modulepicselector.R;
import com.example.modulepicselector.internal.entity.Item;
import com.example.modulepicselector.listener.OnSelectPicCallback;


import org.jetbrains.annotations.NotNull;


import java.util.List;


public class PicSelectAdapter extends RecyclerView.Adapter<PicSelectAdapter.ViewHolder> {

    private OnSelectPicCallback mOnSelectPicCallback;
    private List<Item> mItems;

    public PicSelectAdapter(List<Item> items) {
        mItems = items;
    }

    @NonNull
    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_pic,parent,false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull PicSelectAdapter.ViewHolder holder, int position) {
        holder.imageView.setImageURI(mItems.get(position).uri);
        holder.imageView.setOnClickListener(v -> mOnSelectPicCallback.changeTab(position));
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    public void setList(List<Item> itemList) {
        mItems = itemList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;


        public ViewHolder(@NonNull @NotNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_pic_select);
        }
    }


    public void setOnSelectPicCallback(OnSelectPicCallback onSelectPicCallback) {
        mOnSelectPicCallback = onSelectPicCallback;
    }

}
